//
//  WelfareViewController.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/2/22.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

class WelfareViewController: TTBaseVC {
    
    fileprivate let WelfareCollectionViewCellID = "WelfareCollectionViewCell"
    
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = CGSize.init(width: (SCREEN_WIDTH - 60) / 2, height: (SCREEN_WIDTH - 60) / 79 * 27)
//        layout.headerReferenceSize = CGSize.init(width: SCREEN_WIDTH, height: 55)
        layout.sectionInset = UIEdgeInsets.init(top: 20, left: 20, bottom: 20, right: 20)
        layout.minimumInteritemSpacing = 20
        layout.minimumLineSpacing = 20
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.register(WelfareCollectionViewCell.self, forCellWithReuseIdentifier: WelfareCollectionViewCellID)
        collectionView.delegate = self
        collectionView.dataSource = self
        return collectionView
    }()
    
    lazy var listenModel: [WelfareItemModel] = [{
        return WelfareItemModel()
        }()]

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "红包"
        
        self.collectionView.frame = FRAME_CGFloat(0, 45, SCREEN_WIDTH, SCREEN_HEIGHT - OFFSET_TOP - OFFSET_BOTTOM - 45)
        self.collectionView.backgroundColor = WhiteBackColor
        self.view.addSubview(self.collectionView)

        setHeaderView()
        setData()
        // Do any additional setup after loading the view.
    }

}

// MARK: *******  数据  ********
extension WelfareViewController {
    func setData() {
        WelfareRequestModel.loadWelfareItemData { (success, mes, model) in
            if success && model != nil {
                self.listenModel = model!
                self.collectionView.reloadData()
            }
        }
    }
}

// MARK: *******  UI  ********
extension WelfareViewController {
    func setHeaderView() {
        let welfare = WelfareHeaderView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 45))
        welfare.checkBlock = { () in
            let vc = WalletVc()
            ISLOGIN_PUSHVIEWCONTROLLER(vc)
        }
        self.view.addSubview(welfare)
    }
}

//  MARK: - ###### 协议方法 ######
extension WelfareViewController:UICollectionViewDelegate,UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.listenModel.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: WelfareCollectionViewCellID, for: indexPath) as! WelfareCollectionViewCell
        cell.backView.sd_setImage(with: URL(string:(StringIncludeChineseToString(self.listenModel[indexPath.row].imageUrl ?? ""))), placeholderImage: IMAGE(name: "welfarePlace"), options: .lowPriority, completed: nil)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        switch self.listenModel[indexPath.row].redirectUri {
        case "收听排行榜":
            let vc = WelfareListenViewController()
            PUSHVIEWCONTROLLER(vc)
        case "收入排行榜":
            let vc = WelfareIncomeViewController()
            PUSHVIEWCONTROLLER(vc)
        case "分享免费听":
            let vc = ShareFreeListenVc()
            ISLOGIN_PUSHVIEWCONTROLLER(vc)
        case "签到红包":
            let vc = SignRedVc()
            ISLOGIN_PUSHVIEWCONTROLLER(vc)
        default:
            if !String.getUserIdToString() {
                let vc = LoginVc()
                PRESENTVIEWCONTROLLER(vc)
            } else {
                if self.listenModel[indexPath.row].type == 1 {
                    let vc = WebVc()
                    vc.urlString = self.listenModel[indexPath.row].redirectUri
                    //                    vc.loadUrl(url: "www.baidu.com.cn")
                    vc.modalTransitionStyle = .crossDissolve
                    self.present(vc, animated: true, completion: nil)
                } else {
                    return
                }
            }
        }
    }
}
